using System;
using System.Collections.Generic;
using System.Diagnostics;
using Rhizome.Contract;
using Rhizome.Contract.Diag;
using Rhizome.Model.Validation;

namespace Rhizome.Model
{
    public class CustomAction<TPlant> : EventActionBase<TPlant>, IValidate
    {
        #region IAction<TEvent> Members

        public override void Process()
        {
            var handler = Act;
        	if (handler == null) return;
        	
			var e = new ActionEventArgs<TPlant>(Plant);
        	handler(this, e);
			if (e.Event != null) EventSink.CastEvent(this, e);
        }

        #endregion

		public event EventHandler<ActionEventArgs<TPlant>> Act;

        public override object GetForwardEvent()
        {
			var handler = Act;
            if (handler == null) return null;
            var e = new ActionEventArgs<TPlant>(Plant);
            handler(this, e);
            return e.Event;
        }

    	#region Implementation of IValidate

    	IEnumerable<IValidationResult> IValidate.GetValidationResults()
    	{
			if (Act == null)
				yield return ValidationResult.Error("Event Act not set");
    	}

    	#endregion
    }

    public sealed class ActionEventArgs<TPlant> : EventArgs
    {
        public ActionEventArgs(TPlant plant)
        {
            Plant = plant;
        }

        /// <summary>
        /// Set value to return with GetForwardEvent or casts this event
        /// </summary>
        public object Event { get; set; }

        /// <summary>
        ///   The Subject of controlling
        /// </summary>
        public TPlant Plant { get; private set; }
    }

	public sealed class CustomAction : CustomAction<object> {}
}